home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / WINSRC20.ZIP / MPMATH_A.ASM < prev    next >
Assembly Source File  |  1990-01-19  |  17KB  |  1,061 lines

  1. TITLE mpmath_a.asm (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
  2. SUBTTL All rights reserved.
  3. ;
  4. ;  Code may be used in any program provided the author is credited
  5. ;    either during program execution or in the documentation.  Source
  6. ;    code may be distributed only in combination with public domain or
  7. ;    shareware source code.  Source code may be modified provided the
  8. ;    copyright notice and this message is left unchanged and all
  9. ;    modifications are clearly documented.
  10. ;
  11. ;    I would appreciate a copy of any work which incorporates this code,
  12. ;    however this is optional.
  13. ;
  14. ;    Mark C. Peterson
  15. ;    128 Hamden Ave., F
  16. ;    Waterbury, CT 06704
  17. ;    (203) 754-1162
  18. ;
  19. ;  Note: Remark statements following floating point commands generally indicate
  20. ;     the FPU stack contents after the command is completed.
  21. ;
  22. ;  References:
  23. ;     80386/80286 Assembly Language Programming
  24. ;        by William H. Murray, III and Chris H. Pappas
  25. ;        Published by Osborne McGraw-Hill, 1986
  26. ;        
  27. ;
  28. ;
  29.  
  30.  
  31. IFDEF ??version
  32. MASM51
  33. QUIRKS
  34. EMUL
  35. ENDIF
  36.  
  37. .model medium, c
  38.  
  39.  
  40. .data
  41.  
  42. extrn cpu:WORD
  43.  
  44.  
  45. MP STRUC
  46.    Exp   DW    0
  47.    Mant  DD    0
  48. MP ENDS
  49.  
  50.  
  51. PUBLIC MPOverflow
  52. MPOverflow  DW        0
  53.  
  54. Ans         MP       <?>
  55. Double      DQ        ? 
  56.  
  57.  
  58. .code
  59.  
  60. .8086
  61.  
  62. fg2MP086    PROC     x:DWORD, fg:WORD
  63.    mov   ax, WORD PTR [x]
  64.    mov   dx, WORD PTR [x+2]
  65.    mov   cx, ax
  66.    or    cx, dx
  67.    jz    ExitFg2MP
  68.  
  69.    mov   cx, 1 SHL 14 + 30
  70.    sub   cx, fg
  71.  
  72.    or    dx, dx
  73.    jns   BitScanRight
  74.  
  75.    or    ch, 80h
  76.    not   ax
  77.    not   dx
  78.    add   ax, 1
  79.    adc   dx, 0
  80.  
  81. BitScanRight:
  82.    shl   ax, 1
  83.    rcl   dx, 1
  84.    dec   cx
  85.    or    dx, dx
  86.    jns   BitScanRight
  87.  
  88. ExitFg2MP:
  89.    mov   Ans.Exp, cx
  90.    mov   WORD PTR Ans.Mant+2, dx
  91.    mov   WORD PTR Ans.Mant, ax
  92.    lea   ax, Ans
  93.    mov   dx, ds
  94.    ret
  95. fg2MP086    ENDP
  96.  
  97.  
  98.  
  99. MPcmp086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  100. LOCAL Rev:WORD, Flag:WORD
  101.    mov   Rev, 0
  102.    mov   Flag, 0
  103.    mov   ax, xExp
  104.    mov   dx, WORD PTR [xMant]
  105.    mov   si, WORD PTR [xMant+2]
  106.    mov   bx, yExp
  107.    mov   cx, WORD PTR [yMant]
  108.    mov   di, WORD PTR [yMant+2]
  109.    or    ax, ax
  110.    jns   AtLeastOnePos
  111.  
  112.    or    bx, bx
  113.    jns   AtLeastOnePos
  114.  
  115.    mov   Rev, 1
  116.    and   ah, 7fh
  117.    and   bh, 7fh
  118.  
  119. AtLeastOnePos:
  120.    cmp   ax, bx
  121.    jle   Cmp1
  122.  
  123.    mov   Flag, 1
  124.    jmp   ChkRev
  125.  
  126. Cmp1:
  127.    je    Cmp2
  128.  
  129.    mov   Flag, -1
  130.    jmp   ChkRev
  131.  
  132. Cmp2:
  133.    cmp   si, di
  134.    jbe   Cmp3
  135.  
  136.    mov   Flag, 1
  137.    jmp   ChkRev
  138.  
  139. Cmp3:
  140.    je    Cmp4
  141.  
  142.    mov   Flag, -1
  143.    jmp   ChkRev
  144.  
  145. Cmp4:
  146.    cmp   dx, cx
  147.    jbe   Cmp5
  148.  
  149.    mov   Flag, 1
  150.    jmp   ChkRev
  151.  
  152. Cmp5:
  153.    je    ChkRev
  154.  
  155.    mov   Flag, -1
  156.  
  157. ChkRev:
  158.     or    Rev, 0
  159.    jz    ExitCmp
  160.  
  161.    neg   Flag
  162.  
  163. ExitCmp:
  164.    mov   ax, Flag
  165.    ret
  166. MPcmp086    ENDP
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. MPmul086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  174.    mov   ax, xExp
  175.    mov   bx, yExp
  176.    xor   ch, ch
  177.    shl   bh, 1
  178.    rcr   ch, 1
  179.    shr   bh, 1
  180.    xor   ah, ch
  181.  
  182.    sub   bx, (1 SHL 14) - 2
  183.    add   ax, bx
  184.    jno   NoOverflow
  185.  
  186. Overflow:
  187.    or    WORD PTR [xMant+2], 0
  188.    jz    ZeroAns
  189.    or    WORD PTR [yMant+2], 0
  190.    jz    ZeroAns
  191.  
  192.    mov   MPOverflow, 1
  193.  
  194. ZeroAns:
  195.    xor   ax, ax
  196.    xor   dx, dx
  197.    mov   Ans.Exp, ax
  198.    jmp   StoreMant
  199.  
  200. NoOverflow:
  201.    mov   Ans.Exp, ax
  202.  
  203.    mov   si, WORD PTR [xMant+2]
  204.    mov   bx, WORD PTR [xMant]
  205.    mov   di, WORD PTR [yMant+2]
  206.    mov   cx, WORD PTR [yMant]
  207.  
  208.    mov   ax, si
  209.    or    ax, bx
  210.    jz    ZeroAns
  211.  
  212.    mov   ax, di
  213.    or    ax, cx
  214.    jz    ZeroAns
  215.  
  216.    mov   ax, cx
  217.    mul   bx
  218.    push  dx
  219.  
  220.    mov   ax, cx
  221.    mul   si
  222.    push  ax
  223.    push  dx
  224.  
  225.    mov   ax, bx
  226.    mul   di
  227.    push  ax
  228.    push  dx
  229.  
  230.    mov   ax, si
  231.    mul   di
  232.    pop   bx
  233.    pop   cx
  234.    pop   si
  235.    pop   di
  236.  
  237.    add   ax, bx
  238.    adc   dx, 0
  239.    pop   bx
  240.    add   di, bx
  241.    adc   ax, 0
  242.    adc   dx, 0
  243.    add   di, cx
  244.    adc   ax, si
  245.    adc   dx, 0
  246.  
  247.    or    dx, dx
  248.    js    StoreMant
  249.  
  250.    shl   di, 1
  251.    rcl   ax, 1
  252.    rcl   dx, 1
  253.    sub   Ans.Exp, 1
  254.    jo    Overflow
  255.  
  256. StoreMant:
  257.    mov   WORD PTR Ans.Mant+2, dx
  258.    mov   WORD PTR Ans.Mant, ax
  259.  
  260.    lea   ax, Ans
  261.    mov   dx, ds
  262.    ret
  263. MPmul086    ENDP
  264.  
  265.  
  266.  
  267. d2MP086     PROC     uses si di, x:QWORD
  268.    mov   dx, WORD PTR [x+6]
  269.    mov   ax, WORD PTR [x+4]
  270.    mov   bx, WORD PTR [x+2]
  271.    mov   cx, WORD PTR [x]
  272.    mov   si, dx
  273.    shl   si, 1
  274.    or    si, bx
  275.    or    si, ax
  276.    or    si, dx
  277.    or    si, cx
  278.    jnz   NonZero
  279.  
  280.    xor   ax, ax
  281.    xor   dx, dx
  282.    jmp   StoreAns
  283.  
  284. NonZero:
  285.    mov   si, dx
  286.    shl   si, 1
  287.    pushf
  288.    mov   cl, 4
  289.    shr   si, cl
  290.    popf
  291.    rcr   si, 1
  292.    add   si, (1 SHL 14) - (1 SHL 10)
  293.  
  294.    mov   di, ax                           ; shl dx:ax:bx 12 bits
  295.    mov   cl, 12
  296.    shl   dx, cl
  297.    shl   ax, cl
  298.    mov   cl, 4
  299.    shr   di, cl
  300.    shr   bx, cl
  301.    or    dx, di
  302.    or    ax, bx
  303.    stc
  304.    rcr   dx, 1
  305.    rcr   ax, 1
  306.  
  307. StoreAns:
  308.    mov   Ans.Exp, si
  309.    mov   WORD PTR Ans.Mant+2, dx
  310.    mov   WORD PTR Ans.Mant, ax
  311.  
  312.    lea   ax, Ans
  313.    mov   dx, ds
  314.    ret
  315. d2MP086     ENDP
  316.  
  317.  
  318.  
  319. MP2d086     PROC     uses si di, xExp:WORD, xMant:DWORD
  320.    sub   xExp, (1 SHL 14) - (1 SHL 10)
  321.    jo    Overflow
  322.  
  323.    mov   bx, xExp
  324.    and   bx, 0111100000000000b
  325.    jz    InRangeOfDouble
  326.  
  327. Overflow:
  328.    mov   MPOverflow, 1
  329.    xor   ax, ax
  330.    xor   dx, dx
  331.    xor   bx, bx
  332.    jmp   StoreAns
  333.  
  334. InRangeOfDouble:
  335.    mov   si, xExp
  336.    mov   ax, si
  337.    mov   cl, 5
  338.    shl   si, cl
  339.    shl   ax, 1
  340.    rcr   si, 1
  341.  
  342.    mov   dx, WORD PTR [xMant+2]
  343.    mov   ax, WORD PTR [xMant]
  344.    shl   ax, 1
  345.    rcl   dx, 1
  346.  
  347.    mov   bx, ax
  348.    mov   di, dx
  349.    mov   cl, 12
  350.    shr   dx, cl
  351.    shr   ax, cl
  352.    mov   cl, 4
  353.    shl   bx, cl
  354.    shl   di, cl
  355.    or    ax, di
  356.    or    dx, si
  357.  
  358. StoreAns:
  359.    mov   WORD PTR Double+6, dx
  360.    mov   WORD PTR Double+4, ax
  361.    mov   WORD PTR Double+2, bx
  362.    xor   bx, bx
  363.    mov   WORD PTR Double, bx
  364.  
  365.    lea   ax, Double
  366.    mov   dx, ds
  367.    ret
  368. MP2d086     ENDP
  369.  
  370.  
  371.  
  372.  
  373. MPadd086    PROC     uses si di, xExp:WORD, xMant:DWORD, yExp:WORD, yMant:DWORD
  374.    mov   si, xExp
  375.    mov   dx, WORD PTR [xMant+2]
  376.    mov   ax, WORD PTR [xMant]
  377.  
  378.    mov   di, yExp
  379.  
  380.    mov   cx, si
  381.    xor   cx, di
  382.    js    Subtract
  383.    jz    SameMag
  384.  
  385.    cmp   si, di
  386.    jg    XisGreater
  387.  
  388.    xchg  si, di
  389.    xchg  dx, WORD PTR [yMant+2]
  390.    xchg  ax, WORD PTR [yMant]
  391.  
  392. XisGreater:
  393.    mov   cx, si
  394.    sub   cx, di
  395.    cmp   cx, 32
  396.    jl    ChkSixteen
  397.    jmp   StoreAns
  398.  
  399. ChkSixteen:
  400.    cmp   cx, 16
  401.    jl    SixteenBitShift
  402.  
  403.    sub   cx, 16
  404.    mov   bx, WORD PTR [yMant+2]
  405.    shr   bx, cl
  406.    mov   WORD PTR [yMant], bx
  407.    mov   WORD PTR [yMant+2], 0
  408.    jmp   SameMag
  409.  
  410. SixteenBitShift:
  411.    mov   bx, WORD PTR [yMant+2]
  412.    shr   WORD PTR [yMant+2], cl
  413.    shr   WORD PTR [yMant], cl
  414.    neg   cl
  415.    add   cl, 16
  416.    shl   bx, cl
  417.    or    WORD PTR [yMant], bx
  418.  
  419. SameMag:
  420.    add   ax, WORD PTR [yMant]
  421.    adc   dx, WORD PTR [yMant+2]
  422.    jc    ShiftCarry
  423.    jmp   StoreAns
  424.  
  425. ShiftCarry:
  426.    rcr   dx, 1
  427.    rcr   ax, 1
  428.    add   si, 1
  429.    jo    Overflow
  430.    jmp   StoreAns
  431.  
  432. Overflow:
  433.    mov   MPOverflow, 1
  434.  
  435. ZeroAns:
  436.    xor   si, si
  437.    xor   ax, ax
  438.    xor   dx, dx
  439.    jmp   StoreAns
  440.  
  441. Subtract:
  442.    xor   di, 8000h
  443.    mov   cx, si
  444.    sub   cx, di
  445.    jnz   DifferentMag
  446.  
  447.    cmp   dx, WORD PTR [yMant+2]
  448.    jg    SubtractNumbers
  449.    jne   SwapNumbers
  450.  
  451.    cmp   ax, WORD PTR [yMant]
  452.    jg    SubtractNumbers
  453.    je    ZeroAns
  454.  
  455. SwapNumbers:   
  456.    xor   si, 8000h
  457.    xchg  ax, WORD PTR [yMant]
  458.    xchg  dx, WORD PTR [yMant+2]
  459.    jmp   SubtractNumbers
  460.  
  461. DifferentMag:
  462.    or    cx, cx
  463.    jns   NoSwap
  464.  
  465.    xchg  si, di
  466.    xchg  ax, WORD PTR [yMant]
  467.    xchg  dx, WORD PTR [yMant+2]
  468.    xor   si, 8000h
  469.    neg   cx
  470.  
  471. NoSwap:
  472.    cmp   cx, 32
  473.    jge   StoreAns
  474.  
  475.    cmp   cx, 16
  476.    jl    SixteenBitShift2
  477.  
  478.    sub   cx, 16
  479.    mov   bx, WORD PTR [yMant+2]
  480.    shr   bx, cl
  481.    mov   WORD PTR [yMant], bx
  482.    mov   WORD PTR [yMant+2], 0
  483.    jmp   SubtractNumbers
  484.  
  485. SixteenBi